﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ComponentModel;
using mpost.SilverlightMultiFileUpload.Core;

namespace SMIL.FileUploadClient
{
    public partial class FileUploadUIControl : UserControl, IDisposable, INotifyPropertyChanged
    {

        public FileUploadUIControl()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(FileUploadUIControl_Loaded);
        }

        void FileUploadUIControl_Loaded(object sender, RoutedEventArgs e)
        {
            UserFile.PropertyChanged += UserFile_PropertyChanged;
        }

        void UserFile_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("Percentage"))
            {
                sbProgressFrame.Value = ((UserFile)sender).Percentage * 100;
                sbProgress.Begin();
                
                bDoc.Opacity = ((UserFile)sender).Percentage;
                sbProgressDoc.Begin();
            }

            else if (e.PropertyName.Equals("PercentageFinished"))
            {
                sbProgressFrame.Value = 100;
                sbProgress.Begin();

                bDoc.Opacity = 1;
                sbProgressDoc.Begin();
            }
        }

        

        public void SetProgress(float percentage)
        {
            progressBar.Value = percentage;
            bDoc.Opacity = (percentage / 100) > 0.1 ? (percentage / 100) : 0.1;
        }
        
        public string FileName { set { tbFileName.Text = value; } }

        public UserFile UserFile { get; set; }


        #region IDisposable Members

        public void Dispose()
        {
            UserFile.PropertyChanged -= UserFile_PropertyChanged;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
